package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.BedMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.constant.DmcConstant;
import com.csun.cmny.provider.model.domain.OmcLevelCare;
import com.csun.cmny.provider.model.domain.OmcOldManSupplement;
import com.csun.cmny.provider.model.dto.BedOldmanQueryDto;
import com.csun.cmny.provider.model.dto.csunbutton.BedOldmanCsunButtonReqDto;
import com.csun.cmny.provider.model.dto.sleep.BedOldmanSleepReqDto;
import com.csun.cmny.provider.model.vo.*;
import com.csun.cmny.provider.pojo.Bed;
import com.csun.cmny.provider.pojo.Room;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.vo.BedNosVo;
import com.csun.cmny.provider.vo.BedVo;
import com.csun.cmny.provider.vo.OldmanBedInfoVo;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service("BedService")
@Transactional(rollbackFor = Exception.class)
public class BedServiceImpl implements BedService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    BedMapper bedMapper;

    @Resource
    private DmcRpcService dmcRpcService;

    @Resource
    private OmcOldManSupplementService omcOldManSupplementService;

    @Resource
    private OmcOldManBillService omcOldManBillService;

    @Resource
    private OmcLevelCareService levelCareService;

    @Override
    public int insert(Bed bed) {
        return bedMapper.insertSelective(bed);
    }

    @Override
    public int deleteById(Integer bedId) {
        return bedMapper.deleteByPrimaryKey(bedId);
    }

    @Override
    public int updateById(Bed bed) {
        return bedMapper.updateByPrimaryKeySelective(bed);
    }

    @Override
    public BedVo selectById(Integer bedId) {
        return bedMapper.selectByPrimaryKey(bedId);
    }

    @Override
    public Bed selectByBed(Bed bed) {
        return bedMapper.selectByBed(bed);
    }

    @Override
    public Bed selectByRidAndBedNo(Bed bed) {
        return bedMapper.selectByRidAndBedNo(bed);
    }

    @Override
    public int insertBatch(List<Bed> bedList) {

        /*for (Bed bed : bedList) {
            //写入护理等级名称
            OmcLevelCare omcLevelCare = levelCareService.selectByKey(bed.getLevelCareId());
            if (PublicUtil.isEmpty(omcLevelCare)){
                throw new OmcBizException(ErrorCodeEnum.OMC10080046);
            }
            bed.setLevelCareName(omcLevelCare.getNursingCare());
        }*/
        return bedMapper.insertBatch(bedList);
    }

    @Override
    public int updateByOid(Integer oldmanId) {
        return bedMapper.updateByOid(oldmanId);
    }

    @Override
    public OldmanBedInfoVo selectByOid(Integer oldmanId) {
        return bedMapper.selectByOid(oldmanId);
    }

    /**
     * query bed oldman device with list
     *
     * @param bedOldmanQueryDto bed oldman query dto
     *
     * @return bed oldman device data vo
     */
    @Override
    public BedOldmanDeviceDataVo queryBedOldmanDeviceWithList(BedOldmanQueryDto bedOldmanQueryDto) {

        BedOldmanDeviceDataVo bedOldmanDeviceDataVo = bedMapper.queryBedOldmanDeviceWithList(bedOldmanQueryDto);
        if (PublicUtil.isEmpty(bedOldmanDeviceDataVo)) {
            return null;
        }

        // 房间Id列表
        List<Integer> roomIdList = Lists.newArrayList();
        // 床位-老人-床垫列表
        List<BedOldmanSleepReqDto> bedOldmanSleepReqDtoList = Lists.newArrayList();
        // 床位-老人-日创紧急按钮列表
        List<BedOldmanCsunButtonReqDto> bedOldmanCsunButtonReqDtoList = Lists.newArrayList();
        // 老人Id列表
        List<Integer> oldmanIdList = Lists.newArrayList();
        bedOldmanDeviceDataVo.getRoomVoList().forEach(roomVo -> {
            roomIdList.add(roomVo.getRoomId());
            roomVo.getBedOldmanVoList().forEach(bedOldmanVo -> {
                bedOldmanSleepReqDtoList
                        .add(new BedOldmanSleepReqDto(bedOldmanVo.getBedId(), bedOldmanVo.getOldmanId()));
                bedOldmanCsunButtonReqDtoList
                        .add(new BedOldmanCsunButtonReqDto(bedOldmanVo.getBedId(), bedOldmanVo.getOldmanId()));
                oldmanIdList.add(bedOldmanVo.getOldmanId());
            });
        });

        //region 获取人感信息
        boolean isBodySensorReq = false;    // 是否有人感信息
        Map<Integer, BodySensorReqVo> BodySensorReqVoMap = Maps.newHashMap();
        if (PublicUtil.isNotEmpty(roomIdList)) {
            List<BodySensorReqVo> bodySensorReqVoList = dmcRpcService.queryBodySensorLocationByRoomIds(roomIdList);
            if (PublicUtil.isNotEmpty(bodySensorReqVoList)) {
                isBodySensorReq = true;
                BodySensorReqVoMap = bodySensorReqVoList.stream()
                        .collect(Collectors.toMap(BodySensorReqVo::getRoomId, a -> a,(k1, k2)->k1));
            }
        }
        //endregion

        //region 获取床垫及数据信息
        boolean isCsunSleepDataReq = false;
        boolean isSleepCareDataReq = false;
        Map<Integer, CsunSleepDataReqVo> bedCsunSleepDataReqVoMap = Maps.newHashMap();
        Map<Integer, CsunSleepDataReqVo> oldmanCsunSleepDataReqVoMap = Maps.newHashMap();
        Map<Integer, SleepCareDataReqVo> bedSleepCareDataReqMap = Maps.newHashMap();
        Map<Integer, SleepCareDataReqVo> oldmanSleepCareDataReqMap = Maps.newHashMap();
        if (PublicUtil.isNotEmpty(bedOldmanSleepReqDtoList)) {
            //region 获取日创智能床垫及数据信息
            List<CsunSleepDataReqVo> csunSleepDataReqVoList =
                    dmcRpcService.queryCsunSleepDataByBedOldmanList(bedOldmanSleepReqDtoList);
            List<SleepCareDataReqVo> sleepCareDataReqVoList =
                    dmcRpcService.querySleepCareDataByBedOldmanList(bedOldmanSleepReqDtoList);
            if (PublicUtil.isNotEmpty(csunSleepDataReqVoList)) {
                isCsunSleepDataReq = true;
                csunSleepDataReqVoList.forEach(csunSleepDataReqVo -> {
                    switch (csunSleepDataReqVo.getBindType()) {
                        case DmcConstant.CSUN_SLEEP_BIND_TYPE_OLDMAN :
                            oldmanCsunSleepDataReqVoMap.put(csunSleepDataReqVo.getOldmanId(), csunSleepDataReqVo);
                            break;
                        case DmcConstant.CSUN_SLEEP_BIND_TYPE_BED :
                            bedCsunSleepDataReqVoMap.put(csunSleepDataReqVo.getBedId(), csunSleepDataReqVo);
                    }
                });
            }
            //endregion
            //region 获取睡眠卫士床垫及数据信息
            if (PublicUtil.isNotEmpty(sleepCareDataReqVoList)) {
                isSleepCareDataReq = true;
                sleepCareDataReqVoList.forEach(sleepCareDataReqVo -> {
                    switch (sleepCareDataReqVo.getBindType()) {
                        case DmcConstant.SLEEP_CARE_BIND_TYPE_OLDMAN :
                            oldmanSleepCareDataReqMap.put(sleepCareDataReqVo.getOldmanId(), sleepCareDataReqVo);
                            break;
                        case DmcConstant.SLEEP_CARE_BIND_TYPE_BED :
                            bedSleepCareDataReqMap.put(sleepCareDataReqVo.getBedId(), sleepCareDataReqVo);
                            break;
                    }
                });
            }
            //endregion
        }
        //endregion

        //region 获取紧急按钮数据
        boolean isCsunButtonReq = false;
        Map<Integer, List<CsunButtonReqVo>> bedCsunButtonReqVoMap = Maps.newHashMap();
        Map<Integer, List<CsunButtonReqVo>> oldmanCsunButtonReqVoMap = Maps.newHashMap();
        List<CsunButtonReqVo> csunButtonReqVoList = Lists.newArrayList();
        if (PublicUtil.isNotEmpty(bedOldmanCsunButtonReqDtoList)) {
            csunButtonReqVoList = dmcRpcService.queryCsunButtonByBedOldmanList(bedOldmanCsunButtonReqDtoList);
            if (PublicUtil.isNotEmpty(csunButtonReqVoList)) {
                isCsunButtonReq = true;
                /**
                 * @date    2019-11-19
                 * @author  chisj
                 * @desc    一个床位有多个按钮数据（床位或老人）
                 */
                csunButtonReqVoList.forEach(csunButtonReqVo -> {
                    List<CsunButtonReqVo> oldmanCsunButtonReqVoList, bedCsunButtonReqVoList;
                    switch (csunButtonReqVo.getBindType()) {
                        /**
                         * @date    2019-11-19
                         * @author  chisj
                         * @desc    之前是老人一对一按钮绑定，目前扩展为老人一对多按钮绑定
                         */
                        case DmcConstant.CSUN_BUTTON_BIND_TYPE_OLDMAN :
                            oldmanCsunButtonReqVoList = oldmanCsunButtonReqVoMap.get(csunButtonReqVo.getOldmanId());
                            if (PublicUtil.isEmpty(oldmanCsunButtonReqVoList)) {
                                oldmanCsunButtonReqVoList = Lists.newArrayList();
                            }
                            oldmanCsunButtonReqVoList.add(csunButtonReqVo);
                            oldmanCsunButtonReqVoMap.put(csunButtonReqVo.getOldmanId(), oldmanCsunButtonReqVoList);
                            break;
                        /**
                         * @date    2019-11-19
                         * @author  chisj
                         * @desc    之前是床位一对一按钮绑定，目前扩展为床位一对多按钮绑定
                         */
                        case DmcConstant.CSUN_BUTTON_BIND_TYPE_BED :
                            bedCsunButtonReqVoList = oldmanCsunButtonReqVoMap.get(csunButtonReqVo.getBedId());
                            if (PublicUtil.isEmpty(bedCsunButtonReqVoList)) {
                                bedCsunButtonReqVoList = Lists.newArrayList();
                            }
                            bedCsunButtonReqVoList.add(csunButtonReqVo);
                            bedCsunButtonReqVoMap.put(csunButtonReqVo.getBedId(), bedCsunButtonReqVoList);
                            break;
                    }
                });
            }
        }
        //endregion

        //region 获取门禁卡信息
        boolean isRfidLabelReq = false;
        Map<Integer, RfidLabelReqVo> rfidLabelReqVoMap = Maps.newHashMap();
        if (PublicUtil.isNotEmpty(oldmanIdList)) {
            List<RfidLabelReqVo> rfidLabelReqVoList =
                    dmcRpcService.queryRfidLabelByOldmanIds(oldmanIdList);
            if (PublicUtil.isNotEmpty(rfidLabelReqVoList)) {
                isRfidLabelReq = true;
                rfidLabelReqVoMap = rfidLabelReqVoList.stream()
                        .collect(Collectors.toMap(RfidLabelReqVo::getOldmanId, a -> a,(k1, k2)->k1));
            }
        }
        //endregion

        //region 欧梦龙人感

        //endregion

        //region 红外人感

        //endregion

        //region 温湿度计
        boolean isHumidistatReq = false;    // 是否有人感信息
        Map<Integer, HumidistatReqVo> humidistatReqVoMap = Maps.newHashMap();
        if (PublicUtil.isNotEmpty(roomIdList)) {
            List<HumidistatReqVo> humidistatReqVoList = dmcRpcService.queryHumidistatByRoomIds(roomIdList);
            if (PublicUtil.isNotEmpty(humidistatReqVoList)) {
                isHumidistatReq = true;
                humidistatReqVoMap = humidistatReqVoList.stream()
                        .collect(Collectors.toMap(HumidistatReqVo::getRoomId, a -> a,(k1, k2)->k1));
            }
        }
        //endregion

        //region 网关
        boolean isGatewayReq = false;    // 是否有人感信息
        Map<Integer, GatewayReqVo> gatewayReqVoMap = Maps.newHashMap();
        if (PublicUtil.isNotEmpty(roomIdList)) {
            List<GatewayReqVo> gatewayReqVoList = dmcRpcService.queryGatewayByRoomIds(roomIdList);
            if (PublicUtil.isNotEmpty(gatewayReqVoList)) {
                isGatewayReq = true;
                gatewayReqVoMap = gatewayReqVoList.stream()
                        .collect(Collectors.toMap(GatewayReqVo::getRoomId, a -> a,(k1, k2)->k1));
            }
        }
        //endregion

        //region 摄像头
        // 1.根据房间Id集合查询摄像头Id
        boolean isCameraReq = false;    // 是否有人感信息
        Map<Integer, CameraReqVo> CameraReqVoMap = Maps.newHashMap();
        if (PublicUtil.isNotEmpty(roomIdList)) {
            List<CameraReqVo> cameraReqVoList = dmcRpcService.queryCameraByRoomIds(roomIdList);
            if (PublicUtil.isNotEmpty(cameraReqVoList)) {
                isCameraReq = true;
                CameraReqVoMap = cameraReqVoList.stream()
                        .collect(Collectors.toMap(CameraReqVo::getRoomId, a -> a,(k1, k2)->k1));
            }
        }
        //endregion


        // 设备Id集合
        List<DeviceReqVo> deviceReqVoList = Lists.newArrayList();

        for (RoomVo roomVo : bedOldmanDeviceDataVo.getRoomVoList()) {
            if (isBodySensorReq) {
                BodySensorReqVo bodySensorReqVo = BodySensorReqVoMap.get(roomVo.getRoomId());
                if (PublicUtil.isNotEmpty(bodySensorReqVo)) {
                    roomVo.setBodySensorReqVoList(bodySensorReqVo.getDeviceReqVoList());
                    deviceReqVoList.addAll(bodySensorReqVo.getDeviceReqVoList());
                }
            }

            /**
             * @date    2020-07-19
             * @author  chisj
             * @desc    新增摄像头
             */
            if (isCameraReq) {
                CameraReqVo cameraReqVo = CameraReqVoMap.get(roomVo.getRoomId());
                if (PublicUtil.isNotEmpty(cameraReqVo)) {
                    // TODO 新增房间中摄像头数据
                    // roomVo.setBodySensorReqVoList(bodySensorReqVo.getDeviceReqVoList());
                    deviceReqVoList.addAll(cameraReqVo.getDeviceReqVoList());
                }
            }

            /**
             * @date    2020-07-19
             * @author  chisj
             * @desc    新增温湿度计
             */
            if (isHumidistatReq) {
                HumidistatReqVo humidistatReqVo = humidistatReqVoMap.get(roomVo.getRoomId());
                if (PublicUtil.isNotEmpty(humidistatReqVo)) {
                    // TODO 新增房间中温湿度计数据
                    // roomVo.setBodySensorReqVoList(bodySensorReqVo.getDeviceReqVoList());
                    deviceReqVoList.addAll(humidistatReqVo.getDeviceReqVoList());
                }
            }

            /**
             * @date    2020-07-20
             * @author  chisj
             * @desc    新增网关
             */
            if (isGatewayReq) {
                GatewayReqVo gatewayReqVo = gatewayReqVoMap.get(roomVo.getRoomId());
                if (PublicUtil.isNotEmpty(gatewayReqVo)) {
                    // TODO 新增房间中网关数据
                    // roomVo.setBodySensorReqVoList(bodySensorReqVo.getDeviceReqVoList());
                    deviceReqVoList.addAll(gatewayReqVo.getDeviceReqVoList());
                }
            }


            for (BedOldmanVo bedOldmanVo : roomVo.getBedOldmanVoList()) {
                if (isCsunSleepDataReq) {
                    CsunSleepDataReqVo bedCsunSleepDataReqVo = bedCsunSleepDataReqVoMap.get(bedOldmanVo.getBedId());
                    CsunSleepDataReqVo oldmanCsunSleepDataReqVo = oldmanCsunSleepDataReqVoMap.get(bedOldmanVo.getOldmanId());
                    bedOldmanVo.setCsunSleepDataReqVo(PublicUtil.isNotEmpty(bedCsunSleepDataReqVo) ? bedCsunSleepDataReqVo : oldmanCsunSleepDataReqVo);
                }
                if (PublicUtil.isNotEmpty(bedOldmanVo.getCsunSleepDataReqVo())) {
                    deviceReqVoList.add(bedOldmanVo.getCsunSleepDataReqVo());
                }
                if (isSleepCareDataReq) {
                    SleepCareDataReqVo bedSleepCareDataReqVo = bedSleepCareDataReqMap.get(bedOldmanVo.getBedId());
                    SleepCareDataReqVo oldmanSleepCareDataReqVo = oldmanSleepCareDataReqMap.get(bedOldmanVo.getOldmanId());
                    bedOldmanVo.setSleepCareDataReqVo(PublicUtil.isNotEmpty(bedSleepCareDataReqVo) ? bedSleepCareDataReqVo : oldmanSleepCareDataReqVo);
                }
                if (PublicUtil.isNotEmpty(bedOldmanVo.getSleepCareDataReqVo())) {
                    deviceReqVoList.add(bedOldmanVo.getSleepCareDataReqVo());
                }
                /**
                 * @date    2019-11-19
                 * @author  chisj
                 * @desc    一个床位有多个按钮数据
                 */
                if (isCsunButtonReq) {
                    List<CsunButtonReqVo> bedCsunButtonReqVo = bedCsunButtonReqVoMap.get(bedOldmanVo.getBedId());
                    List<CsunButtonReqVo> oldmanCsunButtonReqVo = oldmanCsunButtonReqVoMap.get(bedOldmanVo.getOldmanId());

                    List<CsunButtonReqVo> csunButtonReqVos = Lists.newArrayList();
                    if (PublicUtil.isNotEmpty(bedCsunButtonReqVo)) {
                        csunButtonReqVos.addAll(csunButtonReqVoList);
                    }
                    if (PublicUtil.isNotEmpty(oldmanCsunButtonReqVo)) {
                        csunButtonReqVos.addAll(oldmanCsunButtonReqVo);
                    }
                    bedOldmanVo.setCsunButtonReqVoList(csunButtonReqVos);

                    // bedOldmanVo.setCsunButtonReqVo(PublicUtil.isNotEmpty(bedCsunButtonReqVo) ? bedCsunButtonReqVo : oldmanCsunButtonReqVo);
                }
                if (PublicUtil.isNotEmpty(bedOldmanVo.getCsunButtonReqVoList())) {
                    deviceReqVoList.addAll(bedOldmanVo.getCsunButtonReqVoList());
                }
                if (isRfidLabelReq) {
                    bedOldmanVo.setRfidLabelReqVo(rfidLabelReqVoMap.get(bedOldmanVo.getOldmanId()));
                }
                if (PublicUtil.isNotEmpty(bedOldmanVo.getRfidLabelReqVo())) {
                    deviceReqVoList.add(bedOldmanVo.getRfidLabelReqVo());
                }
            }
        }

        // 设置redis
        redisTemplate.opsForValue().set(RedisKeyUtil
                .getOmcBuildFloor(bedOldmanQueryDto.getBuildingId(), bedOldmanQueryDto.getFloorNo()), deviceReqVoList);

        return bedOldmanDeviceDataVo;
    }

    @Override
    public BedOldmanDeviceDataVo queryBedOldmanDeviceByGroupId(BedOldmanQueryDto bedOldmanQueryDto) {

        List<BedOldmanInfoVo> bedOldmanInfoVoList = bedMapper.queryBedOldmanDeviceByGroupId(bedOldmanQueryDto);
        if (PublicUtil.isEmpty(bedOldmanInfoVoList)) {
            return null;
        }

        //创建返回对象
        BedOldmanDeviceDataVo bedOldmanDeviceDataVo = new BedOldmanDeviceDataVo();

        // 房间Id列表
        Set<Integer> roomIdSet = Sets.newHashSet();
        // 床位-老人-床垫列表
        List<BedOldmanSleepReqDto> bedOldmanSleepReqDtoList = Lists.newArrayList();
        // 床位-老人-日创紧急按钮列表
        List<BedOldmanCsunButtonReqDto> bedOldmanCsunButtonReqDtoList = Lists.newArrayList();
        // 老人Id列表
        List<Integer> oldmanIdList = Lists.newArrayList();
        //房间hashMap key为房间id value为BedOldmanInfoVo
        HashMap<Integer , BedOldmanInfoVo> bedOldmanInfoVoHashMap = new HashMap<>();

        bedOldmanInfoVoList.forEach(vo-> {
            roomIdSet.add(vo.getRoomId());
            bedOldmanSleepReqDtoList.add(new BedOldmanSleepReqDto(vo.getBedId().intValue(), vo.getOldmanId().intValue()));
            bedOldmanCsunButtonReqDtoList.add(new BedOldmanCsunButtonReqDto(vo.getBedId().intValue(), vo.getOldmanId().intValue()));
            oldmanIdList.add(vo.getOldmanId().intValue());
            bedOldmanInfoVoHashMap.put(vo.getRoomId(),vo);
        });

        //组装数据
        List<RoomVo> roomVoList = Lists.newArrayList();
        for (Integer roomId : roomIdSet) {
            //创建房间vo
            RoomVo roomVo = new RoomVo();
            BedOldmanInfoVo value = bedOldmanInfoVoHashMap.get(roomId);
            //放入房间信息
            roomVo.setRoomId(roomId);
            roomVo.setRoomNo(value.getRoomNo());
            roomVo.setFloorNo(value.getFloorNo());
            List<BedOldmanVo> bedOldmanVoList = Lists.newArrayList();
            //放入床位信息
            for(BedOldmanInfoVo bedOldmanInfoVo : bedOldmanInfoVoList){
                if(bedOldmanInfoVo.getRoomId().equals(roomId)){
                    BedOldmanVo bedOldmanVo = new BedOldmanVo();
                    BeanUtils.copyProperties(bedOldmanInfoVo,bedOldmanVo);
                    bedOldmanVoList.add(bedOldmanVo);
                }
            }
            roomVo.setBedOldmanVoList(bedOldmanVoList);
            roomVoList.add(roomVo);
        }

        //房间id列表
        List<Integer> roomIdList = Lists.newArrayList(roomIdSet);
        bedOldmanDeviceDataVo.setRoomVoList(roomVoList);

        //region 获取人感信息
        boolean isBodySensorReq = false;    // 是否有人感信息
        Map<Integer, BodySensorReqVo> BodySensorReqVoMap = Maps.newHashMap();
        if (PublicUtil.isNotEmpty(roomIdList)) {
            List<BodySensorReqVo> bodySensorReqVoList = dmcRpcService.queryBodySensorLocationByRoomIds(roomIdList);
            if (PublicUtil.isNotEmpty(bodySensorReqVoList)) {
                isBodySensorReq = true;
                BodySensorReqVoMap = bodySensorReqVoList.stream()
                        .collect(Collectors.toMap(BodySensorReqVo::getRoomId, a -> a,(k1, k2)->k1));
            }
        }
        //endregion

        //region 获取床垫及数据信息
        boolean isCsunSleepDataReq = false;
        boolean isSleepCareDataReq = false;
        Map<Integer, CsunSleepDataReqVo> bedCsunSleepDataReqVoMap = Maps.newHashMap();
        Map<Integer, CsunSleepDataReqVo> oldmanCsunSleepDataReqVoMap = Maps.newHashMap();
        Map<Integer, SleepCareDataReqVo> bedSleepCareDataReqMap = Maps.newHashMap();
        Map<Integer, SleepCareDataReqVo> oldmanSleepCareDataReqMap = Maps.newHashMap();
        if (PublicUtil.isNotEmpty(bedOldmanSleepReqDtoList)) {
            //region 获取日创智能床垫及数据信息
            List<CsunSleepDataReqVo> csunSleepDataReqVoList =
                    dmcRpcService.queryCsunSleepDataByBedOldmanList(bedOldmanSleepReqDtoList);
            List<SleepCareDataReqVo> sleepCareDataReqVoList =
                    dmcRpcService.querySleepCareDataByBedOldmanList(bedOldmanSleepReqDtoList);
            if (PublicUtil.isNotEmpty(csunSleepDataReqVoList)) {
                isCsunSleepDataReq = true;
                csunSleepDataReqVoList.forEach(csunSleepDataReqVo -> {
                    switch (csunSleepDataReqVo.getBindType()) {
                        case DmcConstant.CSUN_SLEEP_BIND_TYPE_OLDMAN :
                            oldmanCsunSleepDataReqVoMap.put(csunSleepDataReqVo.getOldmanId(), csunSleepDataReqVo);
                            break;
                        case DmcConstant.CSUN_SLEEP_BIND_TYPE_BED :
                            bedCsunSleepDataReqVoMap.put(csunSleepDataReqVo.getBedId(), csunSleepDataReqVo);
                    }
                });
            }
            //endregion
            //region 获取睡眠卫士床垫及数据信息
            if (PublicUtil.isNotEmpty(sleepCareDataReqVoList)) {
                isSleepCareDataReq = true;
                sleepCareDataReqVoList.forEach(sleepCareDataReqVo -> {
                    switch (sleepCareDataReqVo.getBindType()) {
                        case DmcConstant.SLEEP_CARE_BIND_TYPE_OLDMAN :
                            oldmanSleepCareDataReqMap.put(sleepCareDataReqVo.getOldmanId(), sleepCareDataReqVo);
                            break;
                        case DmcConstant.SLEEP_CARE_BIND_TYPE_BED :
                            bedSleepCareDataReqMap.put(sleepCareDataReqVo.getBedId(), sleepCareDataReqVo);
                            break;
                    }
                });
            }
            //endregion
        }
        //endregion

        //region 获取紧急按钮数据
        boolean isCsunButtonReq = false;
        Map<Integer, List<CsunButtonReqVo>> bedCsunButtonReqVoMap = Maps.newHashMap();
        Map<Integer, List<CsunButtonReqVo>> oldmanCsunButtonReqVoMap = Maps.newHashMap();
        List<CsunButtonReqVo> csunButtonReqVoList = Lists.newArrayList();
        if (PublicUtil.isNotEmpty(bedOldmanCsunButtonReqDtoList)) {
            csunButtonReqVoList = dmcRpcService.queryCsunButtonByBedOldmanList(bedOldmanCsunButtonReqDtoList);
            if (PublicUtil.isNotEmpty(csunButtonReqVoList)) {
                isCsunButtonReq = true;
                /**
                 * @date    2019-11-19
                 * @author  chisj
                 * @desc    一个床位有多个按钮数据（床位或老人）
                 */
                csunButtonReqVoList.forEach(csunButtonReqVo -> {
                    List<CsunButtonReqVo> oldmanCsunButtonReqVoList, bedCsunButtonReqVoList;
                    switch (csunButtonReqVo.getBindType()) {
                        /**
                         * @date    2019-11-19
                         * @author  chisj
                         * @desc    之前是老人一对一按钮绑定，目前扩展为老人一对多按钮绑定
                         */
                        case DmcConstant.CSUN_BUTTON_BIND_TYPE_OLDMAN :
                            oldmanCsunButtonReqVoList = oldmanCsunButtonReqVoMap.get(csunButtonReqVo.getOldmanId());
                            if (PublicUtil.isEmpty(oldmanCsunButtonReqVoList)) {
                                oldmanCsunButtonReqVoList = Lists.newArrayList();
                            }
                            oldmanCsunButtonReqVoList.add(csunButtonReqVo);
                            oldmanCsunButtonReqVoMap.put(csunButtonReqVo.getOldmanId(), oldmanCsunButtonReqVoList);
                            break;
                        /**
                         * @date    2019-11-19
                         * @author  chisj
                         * @desc    之前是床位一对一按钮绑定，目前扩展为床位一对多按钮绑定
                         */
                        case DmcConstant.CSUN_BUTTON_BIND_TYPE_BED :
                            bedCsunButtonReqVoList = oldmanCsunButtonReqVoMap.get(csunButtonReqVo.getBedId());
                            if (PublicUtil.isEmpty(bedCsunButtonReqVoList)) {
                                bedCsunButtonReqVoList = Lists.newArrayList();
                            }
                            bedCsunButtonReqVoList.add(csunButtonReqVo);
                            bedCsunButtonReqVoMap.put(csunButtonReqVo.getBedId(), bedCsunButtonReqVoList);
                            break;
                    }
                });
            }
        }
        //endregion

        //region 获取门禁卡信息
        boolean isRfidLabelReq = false;
        Map<Integer, RfidLabelReqVo> rfidLabelReqVoMap = Maps.newHashMap();
        if (PublicUtil.isNotEmpty(oldmanIdList)) {
            List<RfidLabelReqVo> rfidLabelReqVoList =
                    dmcRpcService.queryRfidLabelByOldmanIds(oldmanIdList);
            if (PublicUtil.isNotEmpty(rfidLabelReqVoList)) {
                isRfidLabelReq = true;
                rfidLabelReqVoMap = rfidLabelReqVoList.stream()
                        .collect(Collectors.toMap(RfidLabelReqVo::getOldmanId, a -> a,(k1, k2)->k1));
            }
        }
        //endregion

        // 设备Id集合
        List<DeviceReqVo> deviceReqVoList = Lists.newArrayList();

        for (RoomVo roomVo : bedOldmanDeviceDataVo.getRoomVoList()) {
            if (isBodySensorReq) {
                BodySensorReqVo bodySensorReqVo = BodySensorReqVoMap.get(roomVo.getRoomId());
                if (PublicUtil.isNotEmpty(bodySensorReqVo)) {
                    roomVo.setBodySensorReqVoList(bodySensorReqVo.getDeviceReqVoList());
                    deviceReqVoList.addAll(bodySensorReqVo.getDeviceReqVoList());
                }
            }
            for (BedOldmanVo bedOldmanVo : roomVo.getBedOldmanVoList()) {
                if (isCsunSleepDataReq) {
                    CsunSleepDataReqVo bedCsunSleepDataReqVo = bedCsunSleepDataReqVoMap.get(bedOldmanVo.getBedId());
                    CsunSleepDataReqVo oldmanCsunSleepDataReqVo = oldmanCsunSleepDataReqVoMap.get(bedOldmanVo.getOldmanId());
                    bedOldmanVo.setCsunSleepDataReqVo(PublicUtil.isNotEmpty(bedCsunSleepDataReqVo) ? bedCsunSleepDataReqVo : oldmanCsunSleepDataReqVo);
                }
                if (PublicUtil.isNotEmpty(bedOldmanVo.getCsunSleepDataReqVo())) {
                    deviceReqVoList.add(bedOldmanVo.getCsunSleepDataReqVo());
                }
                if (isSleepCareDataReq) {
                    SleepCareDataReqVo bedSleepCareDataReqVo = bedSleepCareDataReqMap.get(bedOldmanVo.getBedId());
                    SleepCareDataReqVo oldmanSleepCareDataReqVo = oldmanSleepCareDataReqMap.get(bedOldmanVo.getOldmanId());
                    bedOldmanVo.setSleepCareDataReqVo(PublicUtil.isNotEmpty(bedSleepCareDataReqVo) ? bedSleepCareDataReqVo : oldmanSleepCareDataReqVo);
                }
                if (PublicUtil.isNotEmpty(bedOldmanVo.getSleepCareDataReqVo())) {
                    deviceReqVoList.add(bedOldmanVo.getSleepCareDataReqVo());
                }
                /**
                 * @date    2019-11-19
                 * @author  chisj
                 * @desc    一个床位有多个按钮数据
                 */
                if (isCsunButtonReq) {
                    List<CsunButtonReqVo> bedCsunButtonReqVo = bedCsunButtonReqVoMap.get(bedOldmanVo.getBedId());
                    List<CsunButtonReqVo> oldmanCsunButtonReqVo = oldmanCsunButtonReqVoMap.get(bedOldmanVo.getOldmanId());

                    List<CsunButtonReqVo> csunButtonReqVos = Lists.newArrayList();
                    if (PublicUtil.isNotEmpty(bedCsunButtonReqVo)) {
                        csunButtonReqVos.addAll(csunButtonReqVoList);
                    }
                    if (PublicUtil.isNotEmpty(oldmanCsunButtonReqVo)) {
                        csunButtonReqVos.addAll(oldmanCsunButtonReqVo);
                    }
                    bedOldmanVo.setCsunButtonReqVoList(csunButtonReqVos);

                    // bedOldmanVo.setCsunButtonReqVo(PublicUtil.isNotEmpty(bedCsunButtonReqVo) ? bedCsunButtonReqVo : oldmanCsunButtonReqVo);
                }
                if (PublicUtil.isNotEmpty(bedOldmanVo.getCsunButtonReqVoList())) {
                    deviceReqVoList.addAll(bedOldmanVo.getCsunButtonReqVoList());
                }
                if (isRfidLabelReq) {
                    bedOldmanVo.setRfidLabelReqVo(rfidLabelReqVoMap.get(bedOldmanVo.getOldmanId()));
                }
                if (PublicUtil.isNotEmpty(bedOldmanVo.getRfidLabelReqVo())) {
                    deviceReqVoList.add(bedOldmanVo.getRfidLabelReqVo());
                }
            }
        }

        if(bedOldmanQueryDto.getBuildingId() == null){
            bedOldmanQueryDto.setBuildingId(0);
        }
        if(bedOldmanQueryDto.getFloorNo() == null){
            bedOldmanQueryDto.setFloorNo(0);
        }

        // 设置redis
        redisTemplate.opsForValue().set(RedisKeyUtil
                .getOmGroupcBuildFloor(bedOldmanQueryDto.getGroupId(),bedOldmanQueryDto.getBuildingId(), bedOldmanQueryDto.getFloorNo()), deviceReqVoList);

        return bedOldmanDeviceDataVo;
    }

    /**
     * update bed statuc by id
     *
     * @param bed
     *
     * @return
     */
    @Override
    public Integer updateStatusById(Bed bed) {

        return bedMapper.updateStatusById(bed);
    }

    @Override
    public List<BedNosVo> selectAllNo(Integer afterBed) {
        return bedMapper.selectAllNo(afterBed);
    }

    /*
     * 集团化床位查询
     * */
    @Override
    public List<BedInfoVo> selectBedInfor(Integer buildingId, Integer floorNo, Integer roomId, Long groupId) {
        List<BedInfoVo> voList = bedMapper.selectBedInfor(buildingId,floorNo,roomId,groupId);
        for(BedInfoVo vo : voList){
            if(vo.getOldmanId() != null){
                OmcOldManSupplement oldManSupplement = new OmcOldManSupplement();
                oldManSupplement.setOldManId(vo.getOldmanId());
                oldManSupplement = omcOldManSupplementService.selectOne(oldManSupplement);
                vo.setIsPayed(oldManSupplement.getIsPayed());
            }
        }
        return voList;
    }

    /**
     * query bed oldman device alert data vo
     *
     * @param bedOldmanQueryDto bed oldman query dto
     * @return bed oldman device alert data vo
     */
    @Override
    public BedOldmanDeviceAlertDataVo queryBedOldmanDeviceAndAlertWithList(BedOldmanQueryDto bedOldmanQueryDto) {

        BedOldmanDeviceAlertDataVo bedOldmanDeviceAlertDataVo = new BedOldmanDeviceAlertDataVo();
        BedOldmanDeviceDataVo bedOldmanDeviceDataVo = queryBedOldmanDeviceWithList(bedOldmanQueryDto);
        if (PublicUtil.isEmpty(bedOldmanDeviceDataVo)) {
            return null;
        }
        BeanUtils.copyProperties(bedOldmanDeviceDataVo, bedOldmanDeviceAlertDataVo);

        List<DeviceReqVo> deviceReqVoList = (List<DeviceReqVo>) redisTemplate
                .opsForValue().get(RedisKeyUtil.getOmcBuildFloor(bedOldmanQueryDto.getBuildingId(),
                        bedOldmanQueryDto.getFloorNo()));

        if (PublicUtil.isNotEmpty(deviceReqVoList)) {
            List<DeviceAlertReqVo> deviceAlertReqVoList = dmcRpcService.queryDeviceAlertByDeviceList(deviceReqVoList);
            bedOldmanDeviceAlertDataVo.setDeviceAlertReqVoList(deviceAlertReqVoList);
        }

        return bedOldmanDeviceAlertDataVo;
    }

    @Override
    public BedOldmanDeviceAlertDataVo queryBedOldmanDeviceAndAlertByGroupId(BedOldmanQueryDto bedOldmanQueryDto) {
        BedOldmanDeviceAlertDataVo bedOldmanDeviceAlertDataVo = new BedOldmanDeviceAlertDataVo();
        BedOldmanDeviceDataVo bedOldmanDeviceDataVo = queryBedOldmanDeviceByGroupId(bedOldmanQueryDto);
        if (PublicUtil.isEmpty(bedOldmanDeviceDataVo)) {
            return null;
        }
        BeanUtils.copyProperties(bedOldmanDeviceDataVo, bedOldmanDeviceAlertDataVo);

        List<DeviceReqVo> deviceReqVoList = (List<DeviceReqVo>) redisTemplate
                .opsForValue().get(RedisKeyUtil.getOmGroupcBuildFloor(bedOldmanQueryDto.getGroupId(),bedOldmanQueryDto.getBuildingId(),
                        bedOldmanQueryDto.getFloorNo()));

        if (PublicUtil.isNotEmpty(deviceReqVoList)) {
            List<DeviceAlertReqVo> deviceAlertReqVoList = dmcRpcService.queryDeviceAlertByDeviceList(deviceReqVoList);
            bedOldmanDeviceAlertDataVo.setDeviceAlertReqVoList(deviceAlertReqVoList);
        }

        return bedOldmanDeviceAlertDataVo;
    }

    @Override
    public int bedUpdateList(List<Bed> bedNoList) {
        return bedMapper.bedUpdateList(bedNoList);
    }

    @Override
    public List<BedInfoVo> queryBedInfo(Integer buildingId, Integer floorNo, Integer roomId,Long groupId, LoginAuthDto loginAuthDto) {
        List<BedInfoVo> voList = bedMapper.queryBedInfo(buildingId,floorNo,roomId,groupId);
        for(BedInfoVo vo : voList){
            if(vo.getOldmanId() != null){
//                OmcOldManBill billQuery = new OmcOldManBill();
//                billQuery.setOldManId(vo.getOldmanId());
//                billQuery.setBillType(1);
//                billQuery = omcOldManBillService.selectOne(billQuery);
//                if(billQuery != null){
//                    vo.setIsPayed(billQuery.getIsPay());
//                }
                /*
                * 2019-11-22
                * #4030【系统测试--床位管理】
                * */
                OmcOldManSupplement oldManSupplement = new OmcOldManSupplement();
                oldManSupplement.setOldManId(vo.getOldmanId());
                oldManSupplement = omcOldManSupplementService.selectOne(oldManSupplement);
                vo.setIsPayed(oldManSupplement.getIsPayed());
            }
        }
        return voList;
    }

    @Override
    public List<BedStatusVo> queryBedStatus(Integer buildingId,LoginAuthDto loginAuthDto) {
        List<BedStatusVo> bedStatusVoList = bedMapper.countBedStatus(buildingId,loginAuthDto.getGroupId());
        return bedStatusVoList;
    }

    @Override
    public Integer updateBedOldmanIdAndStatus(Bed bed) {
        return bedMapper.updateByPrimaryKey(bed);
    }

    /**
     * 清除床位上的老人，并且修改床位状态
     *
     * @param bedId
     * @return
     */
    @Override
    public Integer cleanOldmanById(Integer bedId) {

        return bedMapper.cleanOldmanById(bedId);
    }

}
